package com.ruoyi.squadweb.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.squadweb.mapper.UserPermissionsChangeLogMapper;
import com.ruoyi.squadweb.domain.UserPermissionsChangeLog;
import com.ruoyi.squadweb.service.IUserPermissionsChangeLogService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 用户权限变动信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@Service
public class UserPermissionsChangeLogServiceImpl implements IUserPermissionsChangeLogService
{
    @Autowired
    private UserPermissionsChangeLogMapper userPermissionsChangeLogMapper;

    /**
     * 查询用户权限变动信息
     *
     * @param id 用户权限变动信息主键
     * @return 用户权限变动信息
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public UserPermissionsChangeLog selectUserPermissionsChangeLogById(Long id)
    {
        return userPermissionsChangeLogMapper.selectUserPermissionsChangeLogById(id);
    }

    /**
     * 查询用户权限变动信息列表
     *
     * @param userPermissionsChangeLog 用户权限变动信息
     * @return 用户权限变动信息
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public List<UserPermissionsChangeLog> selectUserPermissionsChangeLogList(UserPermissionsChangeLog userPermissionsChangeLog)
    {
        //获取登录用户名
        boolean admin = SecurityUtils.isAdmin(getUserId());
        if (admin) {
            return userPermissionsChangeLogMapper.selectUserPermissionsChangeLogList(userPermissionsChangeLog);
        }else {
            String username = getUsername();
            userPermissionsChangeLog.setSteamid(username);
            List<UserPermissionsChangeLog> userPermissionsChangeLogs = userPermissionsChangeLogMapper.selectUserPermissionsChangeLogList(userPermissionsChangeLog);
            if (userPermissionsChangeLogs.size() < 1) {
                throw new RuntimeException("您没有权限查看该steamId信息或改steamId没有信息，steamId为你的用户名");
            }
            return userPermissionsChangeLogs;
        }
    }

    /**
     * 新增用户权限变动信息
     *
     * @param userPermissionsChangeLog 用户权限变动信息
     * @return 结果
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int insertUserPermissionsChangeLog(UserPermissionsChangeLog userPermissionsChangeLog)
    {
        return userPermissionsChangeLogMapper.insertUserPermissionsChangeLog(userPermissionsChangeLog);
    }

    /**
     * 修改用户权限变动信息
     *
     * @param userPermissionsChangeLog 用户权限变动信息
     * @return 结果
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int updateUserPermissionsChangeLog(UserPermissionsChangeLog userPermissionsChangeLog)
    {
        return userPermissionsChangeLogMapper.updateUserPermissionsChangeLog(userPermissionsChangeLog);
    }

    /**
     * 批量删除用户权限变动信息
     *
     * @param ids 需要删除的用户权限变动信息主键
     * @return 结果
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int deleteUserPermissionsChangeLogByIds(Long[] ids)
    {
        return userPermissionsChangeLogMapper.deleteUserPermissionsChangeLogByIds(ids);
    }

    /**
     * 删除用户权限变动信息信息
     *
     * @param id 用户权限变动信息主键
     * @return 结果
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int deleteUserPermissionsChangeLogById(Long id)
    {
        return userPermissionsChangeLogMapper.deleteUserPermissionsChangeLogById(id);
    }
}
